/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientechat;

import java.io.IOException;

/**
 *
 * @author Administrador
 */
public class Sesion extends Thread {

    private VentanaPrincipal clienteChat;
    private boolean conexion_establecida;

    public Sesion(VentanaPrincipal clienteChat) throws IOException {
        this.clienteChat = clienteChat;
        start();
    }

    public void run() {
        String fin1 = "Adios";
        String fin2 = "Hasta la vista";
        String linea = null;
        try {
             
            while ((linea = clienteChat.entrada.readLine()) != null) {
                if(!conexion_establecida){
                    clienteChat.habilita();
                    conexion_establecida=true;
                }
                clienteChat.insertaLinea(linea);
                //clienteChat.getJtaTexto().setText(clienteChat.getJtaTexto().getText() + linea + System.getProperty("line.separator"));
                if (linea.equals(fin1) || linea.equals(fin2)) {
                    try {
                        this.sleep(5000);
                    } catch (java.lang.InterruptedException e1) {
                    }
                    break;
                }
            }
        } catch (IOException e2) {
            e2.printStackTrace();
        } finally {
            try {
                clienteChat.dispose();
                clienteChat.entrada.close();
                clienteChat.salida.close();
                clienteChat.socketCliente.close();
            } catch (IOException e3) {
            }
            System.exit(-1);
        }
    }
}
